<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>array(0,1,2));
    $orderby='paixu desc';
	$total='';	
	if(!empty($_RQ['goodsid'])){
		$params['id']=intval($_RQ['goodsid']);
		$return['params']['id']=intval($_RQ['goodsid']);
	}
	$return['list']=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,array('id','title','status','total','price','image','salenum','api_data'),'',$orderby);
	foreach($return['list'] as &$v){
		$v['isoptions']=pdo_count('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$v['id']));
		$v['api_data']=json_decode($v['api_data'],true);
		$v['apisn']=$v['api_data']['sn'];
		$v['apiwfee']=$v['api_data']['wfee'];
	}
	paixu($return['list'],'salenum','desc');
	$htmls=array(
		'type'=>'list',	
		'tabs'=>array(
			array('op'=>'list','title'=>'库存管理'),
		),
		'list'=>array('data'=>'list','tip'=>'一键同步：会把所有商品加入门店库存为0（新加商品也会同步上去）要追加库存，需要操作【门店调拨】；一键重置：会重置所有门店库存的设置，恢复跟平台一直！;规格的库存需单独设置！','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'id','title'=>'商品ID','type'=>'string'),
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'price','title'=>'平台价格','type'=>'string'),
			array('name'=>'total','title'=>'平台库存','type'=>'string'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_erp/ku.goodsku/setstatus0/','msg'=>'确定一键下架商品？','title'=>'一键下架'),
			array('type'=>'post','url'=>'jy_erp/ku.goodsku/setstatus1/','msg'=>'确定一键上架商品？','title'=>'一键上架'),
		)),
		'search'=>array(			
			array('name'=>'title','width'=>15,'title'=>'商品名称','type'=>'string'),
		)
	);
	$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
	//$return['store']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='setstatus0'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('下架成功！');
}
if($_RQ['op']=='setstatus1'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择商品','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	
	exi('上架成功！');
}
